<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.SQLException" %> <%--
Created by IntelliJ IDEA.
  User: 自由如风
  Date: 2018/3/26
  Time: 18:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<head>
    <title>添加用户</title>
    <style type="text/css">
        a {
            font-size: 5px;
            color: blueviolet;
            text-decoration: underline;
        }

        body {
            font-size: 8px;
        }

        input {
            height: 8px;
            width: 30px;
            font-size: 5px;
        }

        #input {
            font-size: 3px;
        }
    </style>
</head>

<a href="userList.jsp">返回列表</a></br>

<form
        method="post">
    用户名:<input name="username" type="text" id="username" class="username"></br>
    密码: <input name="password" type="text" id="password" class="password"></br>
    <input type="submit" id="input" value="添加">
</form>

<%
    request.setCharacterEncoding("UTF-8");
    String username = request.getParameter("username");
    String pass = request.getParameter("password");

    if (username == null || pass == null) {

    } else {
        //jdbc技术访问数据库
        Connection connection = null;
        Statement statement = null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone = UTC&&characterEncoding = UTF-8", "root", "193749");
            statement = connection.createStatement();
            statement.executeUpdate("INSERT INTO t_users(username,password) VALUES('" + username + "','" + pass + "');");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


%>
</body>
</html>
